package com.dlry.db;

import java.util.List;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.dlry.entity.DlLeave;
import com.dlry.entity.DlLoginuser;
import com.dlry.entity.DlNotification;
import com.dlry.entity.DlTravelRequest;

public interface UserRepository extends JpaRepository<DlLoginuser, Long>, UserProperitiesPaged {

	Page<DlLoginuser> findAll(Pageable arg0);

	long count();

	void delete(DlLoginuser arg0);

	void delete(Iterable<? extends DlLoginuser> arg0);

	void delete(Long arg0);

	void deleteAll();

	boolean exists(Long arg0);

	DlLoginuser findOne(Long arg0);

	<S extends DlLoginuser> S save(S arg0);

	<S extends DlLoginuser> long count(Example<S> arg0);

	<S extends DlLoginuser> boolean exists(Example<S> arg0);

	<S extends DlLoginuser> Page<S> findAll(Example<S> arg0, Pageable arg1);

	<S extends DlLoginuser> S findOne(Example<S> arg0);

	List<DlLoginuser> findAll();

	List<DlLoginuser> findAll(Sort sort);

	List<DlLoginuser> findAll(Iterable<Long> ids);

	<S extends DlLoginuser> List<S> save(Iterable<S> entities);

	void flush();

	<S extends DlLoginuser> S saveAndFlush(S entity);

	<S extends DlLoginuser> List<S> findAll(Example<S> example);

	<S extends DlLoginuser> List<S> findAll(Example<S> example, Sort sort);

	DlLoginuser findByName(String name);
	// // 自定义查询方法
	// @Query("select n from DlLoginuser u inner join u.notifications as n where
	// u.id = ?1 order by n.updatetime desc") // 获取已发通知
	// List<DlNotification> getNotificationsById(Long uid);

	// @Query("select l from DlLoginuser u inner join u.leaves as l where u.id =
	// ?1") // 获取请假记录
	// List<DlLeave> getLeavesById(Long uId);

	// @Query("select t from DlLoginuser u inner join u.travels as t where u.id
	// = ?1") // 获取出差记录
	// List<DlTravelRequest> getTravelRequestById(Long uId);
	// @Query("select r from DlLoginuser u inner join u.rooms as r where u.id =
	// ?1") // 获取名下会议室
	// List<DlMeetingRoom> getRoomsById(Long uId);
	@Query("select count(*) from DlLoginuser u inner join u.leaves as l where u.id = ?1")
	Long getLeaveCountById(Long uId);

	@Query("select count(*) from DlLoginuser u inner join u.notifications as n  where u.id = ?1")
	Long getNotificationCountById(Long uId);

	@Query("select count(*) from DlLoginuser u inner join u.travels as l where u.id = ?1")
	Long getTravelRequestCountById(Long uId);

	@Query("select count(*) from DlLoginuser u inner join u.rooms as l where u.id = ?1")
	Long getMeetingRoomCountById(Long uId);

	List<DlNotification> getNotificationsPagedById(Long uid, Integer from, Integer max);

	List<DlLeave> getLeavesPagedById(Long uId, Integer from, Integer max);

	List<DlTravelRequest> getTravelPagedById(Long uId, Integer from, Integer max);

	@Modifying
	@Query("update DlLoginuser u set u.password = ?2 where u.id = ?1")
	void changeUserPassword(Long uId, String password);

	@Query(nativeQuery = true, value = "SELECT * FROM dl_loginuser AS u WHERE  u.branch_id = ?1 ORDER BY u.level DESC LIMIT 1;")
	DlLoginuser getLeader(Long bId);

}
